//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.data.biz.utils.servlet;

import com.data.biz.utils.JSONUtilsEx;
import com.data.biz.utils.ServiceException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class ServletInfo {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Map<String, Object> paramMap = new HashMap();
    private Map<String, String> clobParamMap = new HashMap();
    private Map<String, String> frameParamMap = new HashMap();
    private static final String FRAME_PARAM_HEAD = "FRAME";
    public static final String ACTION_MODE_AJAX = "0";
    public static final String ACTION_MODE_DOWNLOAD = "2";

    public String getServiceName() {
        return (String)this.frameParamMap.get("serviceName");
    }

    public String getInvokeSeqno() {
        return (String)this.frameParamMap.get("invokeSeqno");
    }

    public String getActionMode() {
        return this.frameParamMap.get("actionMode") == null ? "0" : (String)this.frameParamMap.get("actionMode");
    }

    public ServletInfo(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.createParamMap();
    }

    private Map<String, Object> createParamMap() {
        if (this.isMultipart()) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List fileItemList = null;

            try {
                fileItemList = upload.parseRequest(this.request);
            } catch (Exception var9) {
                throw new ServiceException(var9);
            }

            Iterator var5 = fileItemList.iterator();

            while(var5.hasNext()) {
                FileItem fileItem = (FileItem)var5.next();
                if (fileItem.isFormField()) {
                    try {
                        this.paramMap.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
                    } catch (UnsupportedEncodingException var8) {
                        throw new ServiceException(var8);
                    }
                } else if (fileItem.getSize() > 0L) {
                    if (!this.paramMap.containsKey(fileItem.getFieldName())) {
                        this.paramMap.put(fileItem.getFieldName(), new ArrayList());
                    }

                    ((List)this.paramMap.get(fileItem.getFieldName())).add(fileItem);
                }
            }
        }

        Enumeration paramNames = this.request.getParameterNames();
        if (paramNames != null) {
            while(true) {
                while(true) {
                    String paramName;
                    do {
                        if (!paramNames.hasMoreElements()) {
                            return this.paramMap;
                        }

                        paramName = (String)paramNames.nextElement();
                    } while(StringUtils.isBlank(paramName));

                    String paramValue = this.request.getParameter(paramName);
                    if (paramName.equals("FRAMEparams")) {
                        Map result = json2Map(paramValue);
                        Iterator var6 = result.keySet().iterator();

                        while(var6.hasNext()) {
                            Object key = var6.next();
                            Object value = result.get(key);
                            this.paramMap.put(String.valueOf(key), value);
                        }
                    } else if (paramName.indexOf("FRAME") == 0) {
                        this.frameParamMap.put(paramName.substring("FRAME".length()), paramValue);
                    } else {
                        this.paramMap.put(paramName, paramValue);
                    }
                }
            }
        } else {
            return this.paramMap;
        }
    }


    public static Map<String, Object> json2Map(String jsonStr) {
        Map<String, Object> paramMap = new HashMap();
        if (!StringUtils.isEmpty(jsonStr) && !"{}".equals(jsonStr)) {
            paramMap = (Map) JSONUtilsEx.deserialize(jsonStr, HashMap.class);
            Iterator var3 = ((Map)paramMap).keySet().iterator();

            while(var3.hasNext()) {
                String key = (String)var3.next();
                if ("null".equals(((Map)paramMap).get(key))) {
                    ((Map)paramMap).put(key, (Object)null);
                }
            }
        }

        return (Map)paramMap;
    }


    public Map<String, Object> getParamMap() {
        return this.paramMap;
    }

    public Map<String, String> getClobParamMap() {
        return this.clobParamMap;
    }

    public Map<String, String> getFrameParamMap() {
        return this.frameParamMap;
    }

    public String getContentPath() {
        return this.request.getContextPath();
    }

    public String getJcaptcha() {
        return (String)this.request.getSession().getAttribute("valiCode");
    }

    public String getServerName() {
        return this.request.getServerName();
    }

    public String getServerPort() {
        return String.valueOf(this.request.getServerPort());
    }

    public String getServerIp() {
        return this.getServerName() + ":" + this.getServerPort();
    }

    public HttpServletRequest getRequest() {
        return this.request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return this.response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public boolean isMultipart() {
        return !StringUtils.isBlank(this.request.getContentType()) && this.request.getContentType().indexOf("multipart/form-data") > -1;
    }
}
